/*
 * @Description: 路由拦截器、nprogress顶部加载效果
 * @Author: Chenx
 * @Date: 2023-03-17 16:03:50
 * @LastEditors: Chenx
 * @LastEditTime: 2023-03-20 17:18:19
 */
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { userStore } from '@/store/modules/user'

NProgress.configure({
  easing: 'ease', // 动画方式
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.4, // 更改启动时使用的最小百分比
})

// 白名单
const whiteList = ['/login', '/redirect']

router.beforeEach((to, form, next) => {
  const userInfo = userStore() // 不要全局调用 因为在 main.ts文件中，是先引入permission.ts文件然后再将pinia挂载到app上的
  NProgress.start()
  console.log('--跳转至:', to, '  userInfo:', userInfo, '  token:', userInfo.getToken)
  if (userInfo.getToken) {
    // 已登录状态
    next()
  } else {
    // Has no token 未登录
    if (whiteList.includes(to.path)) {
      // In the free login whitelist, go directly
      next()
    } else {
      // Other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})